var selectValue="",iDisplayStart=0,iDisplayLength=10,totalPageNum,pageNum=0,pageFlag=false,getHamid111="",getHamidFlag=false;
//分页初始化数据;
var pagition=function(obj,start){
	var stratDate=$("#stratDate").val();
	var endDate=$("#endDate").val();
	if(stratDate=='' && endDate==''){
		  layer.msg("请选择时间!")
		  return ;
	}
	$.ajax({
		type:"POST",
		contentType : "application/x-www-form-urlencoded; charset=UTF-8", 
		url:baseURL+'/api/safeguard/listGroup?flightNum='+encodeURIComponent(obj)+'&iDisplayStart='+start*iDisplayLength+'&iDisplayLength='+iDisplayLength+'&status=1&beginDate='+stratDate+"&endDate="+endDate,
		dataType:"JSON",
		success:function(result){
			if(result.flag){
				if(result.results.length!=0){
					$(".pagitionList").empty();
					$.each(result.results,function(i,n){
						$(".pagitionList").append("<li value='"+n.flightNum+"' hamID='"+n.hamID+"' name='"+n.flightNum+"' status='"+n.status+"'>"+n.flightNum+"</li>")
					})
					$(".pagitionList>li").on('click',function(e){
						e.stopPropagation();
						$("#groupName").val($(e.target).text());
						localStorage.getHamid=$(e.target).attr("hamID");
						$(".pagitionList>li").removeClass("airportSelectBK");
						$(e.target).addClass("airportSelectBK");
					})
				}else {
					$(".pagitionList").empty().append("<li>没有数据</li>")
				}
				$('.pagitionDiv').show();
			}else layer.msg("数据报错："+result.message);
			totalPageNum=Math.floor(result.totalcount/iDisplayLength);
			if(totalPageNum>0){
				$('.pagitionController').show();	
			}else $('.pagitionController').hide();
		}
	  })
	}

$(document).ready(function(){
	$('.table-sort').dataTable(pageStting());
	$('#DataTables_Table_0_length').find('label').contents().wrap("<span class='f-l mr-10'></span>");
	$('.nextPagition').on('click',function(e){
		e.stopPropagation();
		++pageNum;
		if(pageNum>totalPageNum){
			pageNum=totalPageNum
			$(e.target).addClass('disabled')
			return;
		}else{
			$(e.target).removeClass('disabled')
			pagition(selectValue,pageNum);
		}
	})
	var isMouse=true;
	$('.prevPagition').on('click',function(e){
		e.stopPropagation();
		--pageNum;
		if(pageNum<=-1){
			pageNum=0;
			$(e.target).addClass('disabled')
			return;
		}else{
			$(e.target).removeClass('disabled')
			pagition(selectValue,pageNum);
		}
	})
	$("#groupName").on('click',function(e){
		pagition($(e.target).val(),0);
		pageNum=0;	
	})
	$("#groupName").on('keyup',function(e){
		pageNum=0;
		pageFlag=true;
		selectValue=$(e.target).val();
		pagition($(e.target).val(),0);
	})
	$('.pagitionDiv').on('mouseleave',function(e){
		$('.pagitionDiv').hide();
		isMouse=true;
	})
	$('.pagitionDiv').on('mouseover',function(e){
		isMouse=false;
	})
	$('#groupName').on('blur',function(e){
		if(isMouse){
			$(e.target).next().hide();
		}
	})
	$('#doSearch').on('click',function(){
		doSearch();
	})
	$('#doReset').on('click',function(){
		$('#groupName,#stratDate,#endDate,#userName').val('');
	})
});
$(window).resize(function(){
	$(".table-sort").css('width','100%');
});
//从服务器分批取数据
pageStting = function(){
		var optsortable = {
					"bPaginate":true,
					"stateSave":true,
					"bProcessing":true,
					"iDisplayLength":10,
					"sAjaxSource":baseURL+'/api/safeguard/queryHistory',
					"sPaginationType":'full_numbers',
					"bServerSide": true,
					"bSort": false,             
					"bFilter": false,  
				     "aoColumnDefs": [] ,
					"fnServerData": function(sSource, aoData, fnCallback){
							var globalSearch = {};
							var searchConditions = {};
							if($("#userName").val()!=""){
								searchConditions.fromUser=$("#userName").val();
							}
							var pagitionListSelect=$(".pagitionList").find('li.airportSelectBK');
							if(pagitionListSelect.length!=0){
								searchConditions.hamid=localStorage.getHamid;
							}
							if($.trim($("#stratDate").val())!=''){
								searchConditions.begintime=$.trim($("#stratDate").val());
							}
							if($.trim($("#endDate").val())!=''){
								searchConditions.endtime=$.trim($("#endDate").val());
							}
							globalSearch.name= 'globalSearch';
							globalSearch.value= JSON.stringify(searchConditions);
							aoData.push(globalSearch);
							 $.ajax({  
						        type: "GET",     
						        url: sSource,     
						        dataType: "json",    
						        contentType: "application/json;charset=UTF-8", 
						        data: aoData, // 以json格式传递  
						        success: function(result) {
						        	if(!result.flag){
					            		if(result.statusCode==503){
					            		  location=baseURL+"/api/tologin";
					            		}else{
					            		  layer.msg("查询失败！");
					            		}
					            	  return;
					            	}
						        	if(result.flag){
							        	var obj =result;
										var data = obj.results;
										var aaData = [];
										var dataResult = {};
										dataResult.iTotalRecords = obj.totalcount;
										dataResult.iTotalDisplayRecords= obj.totalcount;
										dataResult.iDisplayStart = obj.offset;
										dataResult.iDisplayLength= obj.limit;
										dataResult.aaData = aaData;
										//json重构数组对象 
										$.each(data,function(i,val){
												aaData[i]=[]
												var objContent=$.parseJSON(val.content);
												if(objContent.content){
													aaData[i].push(getContent($('#groupName').val()));
													aaData[i].push(getContent(val.from));
													aaData[i].push(getTime(objContent.datetime));
													aaData[i].push(getContent(objContent.content));
												}else{
													aaData[i].push("系统消息");
													aaData[i].push("系统消息");
													aaData[i].push("系统消息");
													aaData[i].push("系统消息");
												}
										});
							            fnCallback(dataResult);   
						          }
						        }    
						     });
					}
				}	
		return optsortable;
}
doSearch = function(){
   if($("#stratDate").val()==""){
	   layer.msg("请选择开始时间!")
	   return 
   }
   if($("#endDate").val()==""){
	   layer.msg("请选择结束时间!")
	   return 
   }
   if($(".pagitionList").find('li.airportSelectBK').length==0){
		   layer.msg("请从下拉列表中选择一个群组名!")
		   return 
   }/*else{
	   $.each($('.pagitionList').find('li'),function(i,n){
		   if($(n).attr('name')==$.trim($("#groupName").val())){
			   getHamid=$(n).attr('hamID');
			   getHamidFlag=true;
		   }
	   })
   }
   if(!getHamidFlag){
	   layer.msg("请选择正确的群组名！")
	   return;
   }*/
   var seachValue={};
   $('.table-sort').DataTable().search(seachValue).draw();
}	
function getTime(t){
	 var date=new Date(t);
	 return date.getFullYear()+"-"+changeTime((date.getMonth()+1))+"-"+changeTime(date.getDate())+"&nbsp;"+changeTime(date.getHours())+":"+changeTime(date.getMinutes());
}
function changeTime(obj){
return obj<10?"0"+obj:obj;
}
function getContent(obj){
	if(obj.length<=80){
		return "<span>"+obj+"</span>";
	}else return "<span>"+obj.substring(0,80)+"...</span>";
}
